package cn.huapu.power.energy.util.wordUtils;

import cn.huapu.power.energy.bean.vo.PeaRankVo;
import cn.huapu.power.energy.ex.ErrorCodesException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.DefaultCategoryDataset;
import sun.misc.BASE64Encoder;

import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author jzx
 * @title: JfImgUtil
 * @projectName energy
 * @description: JFreeChart工具生成图片
 * @date 2019-8-29:38
 */
public class JfImgUtil {
    public static void main( String[ ] args )throws Exception
    {


        DefaultCategoryDataset dataset = new DefaultCategoryDataset( );

        dataset.setValue( 6 , "得分" , "华普" );
        dataset.setValue( 5 , "得分" , "5" );
        dataset.setValue( 4 , "得分" , "4" );
        dataset.setValue(  3, "得分" , "3" );
        dataset.setValue( 2 , "得分" , "2" );
        dataset.setValue( 1 , "得分" , "1" );


    JFreeChart barChart = ChartFactory.createBarChart(
                "",
                "", "",
                dataset,PlotOrientation.VERTICAL,
                true, true, false);


        CategoryPlot plot = barChart.getCategoryPlot();
        BarRenderer renderer = new BarRenderer();
        // 设置柱子宽度
        renderer.setMaximumBarWidth(0.05);
        // 设置柱子高度
        renderer.setMinimumBarLength(0.2);
        // 设置柱的颜色
        renderer.setSeriesPaint(0, Color.decode("#24F4DB")); // 给series1 Bar
        renderer.setSeriesPaint(1, Color.decode("#7979FF")); // 给series2 Bar
        renderer.setSeriesPaint(2, Color.decode("#FF5555")); // 给series3 Bar
        renderer.setSeriesPaint(3, Color.decode("#F8D661")); // 给series4 Bar
        renderer.setSeriesPaint(4, Color.decode("#F284DC")); // 给series5 Bar
        renderer.setSeriesPaint(5, Color.decode("#00BF00")); // 给series6 Bar
        //大多数情况下都需要在柱子上显示对应的数值，设置如下
        renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer.setBaseItemLabelsVisible(true);
        renderer.setBaseItemLabelPaint(Color.BLUE);//设置数值颜色，默认黑色
        renderer.setItemLabelAnchorOffset(2);



        barChart.getTitle().setFont(new Font("隶书", Font.ITALIC, 15));//设置标题
        //设置图例类别字体
        barChart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 15));

        barChart.setBackgroundPaint(Color.WHITE);
        CategoryPlot categoryPlot=barChart.getCategoryPlot();//用于设置显示特性
        categoryPlot.setBackgroundPaint(Color.WHITE);
        categoryPlot.setDomainGridlinePaint(Color.BLACK);//分类轴网格线条颜色
        categoryPlot.setDomainGridlinesVisible(true);
        categoryPlot.setRangeGridlinePaint(Color.GREEN);//数据轴网格线条颜色

        CategoryAxis domainAxis=categoryPlot.getDomainAxis(); //水平底部列表
        domainAxis.setLabelFont(new Font("黑体",Font.BOLD,5)); //水平底部标题
        domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,10)); //垂直标题
        ValueAxis rangeAxis=categoryPlot.getRangeAxis();//获取柱状
        rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,5)); //设置柱状标题

        // 设置柱图背景色（注意，系统取色的时候要使用16位的模式来查看颜色编码，这样比较准确）
        plot.setBackgroundPaint(new Color(255, 255, 204));



        plot.setRenderer(renderer);
        File BarChart = new File( "D:/doc_f/img/raking.jpg" );
        ChartUtilities.saveChartAsJPEG( BarChart , barChart , 200 , 300 );
    }




    /**
    * @Author jzx
    * @Description //根据排名数据生成图片
    * @Date 18:13 2019-8-2
    * @Param [list]
    * @return java.lang.String
    **/
    public static File getImg(List<PeaRankVo> list) throws IOException {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset( );

        for (int i = 0; i < list.size(); i++) {
            dataset.setValue( list.get(i).getScore(), list.get(i).getScore(), list.get(i).getClientname() );
        }

        JFreeChart barChart = ChartFactory.createBarChart(
                "",
                "", "",
                dataset,PlotOrientation.VERTICAL,
                true, true, false);


        CategoryPlot plot = barChart.getCategoryPlot();
        BarRenderer renderer = new BarRenderer();
        // 设置柱子宽度
        renderer.setMaximumBarWidth(0.05);
        // 设置柱子高度
        renderer.setMinimumBarLength(0.2);
        // 设置柱的颜色
        renderer.setSeriesPaint(0, Color.decode("#7979FF")); // 给series1 Bar
        renderer.setSeriesPaint(1, Color.decode("#7979FF")); // 给series2 Bar
        renderer.setSeriesPaint(2, Color.decode("#7979FF")); // 给series3 Bar
        renderer.setSeriesPaint(3, Color.decode("#7979FF")); // 给series4 Bar
        renderer.setSeriesPaint(4, Color.decode("#7979FF")); // 给series5 Bar
        renderer.setSeriesPaint(5, Color.decode("#24F4DB")); // 给series6 Bar
        //大多数情况下都需要在柱子上显示对应的数值，设置如下
        renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
        renderer.setBaseItemLabelsVisible(true);
        renderer.setBaseItemLabelPaint(Color.BLUE);//设置数值颜色，默认黑色
        renderer.setItemLabelAnchorOffset(2);



        barChart.getTitle().setFont(new Font("隶书", Font.ITALIC, 15));//设置标题
        //设置图例类别字体
        barChart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 15));

        barChart.setBackgroundPaint(Color.WHITE);
        CategoryPlot categoryPlot=barChart.getCategoryPlot();//用于设置显示特性
        categoryPlot.setBackgroundPaint(Color.WHITE);
        categoryPlot.setDomainGridlinePaint(Color.BLACK);//分类轴网格线条颜色
        categoryPlot.setDomainGridlinesVisible(true);
        categoryPlot.setRangeGridlinePaint(Color.GREEN);//数据轴网格线条颜色

        CategoryAxis domainAxis=categoryPlot.getDomainAxis(); //水平底部列表
        domainAxis.setLabelFont(new Font("黑体",Font.BOLD,5)); //水平底部标题
        domainAxis.setTickLabelFont(new Font("宋体",Font.BOLD,10)); //垂直标题
        ValueAxis rangeAxis=categoryPlot.getRangeAxis();//获取柱状
        rangeAxis.setLabelFont(new Font("黑体",Font.BOLD,5)); //设置柱状标题

        // 设置柱图背景色（注意，系统取色的时候要使用16位的模式来查看颜色编码，这样比较准确）
        plot.setBackgroundPaint(new Color(255, 255, 204));



        plot.setRenderer(renderer);
        File BarChart = new File( "D:/doc_f/img/raking.jpg" );
        ChartUtilities.saveChartAsJPEG( BarChart , barChart , 500 , 350 );
        return BarChart;
    }

    /**
    * @Author jzx
    * @Description //图片转化成base64字符串
    * @Date 13:49 2019-8-3
    * @Param [file]
    * @return java.lang.String
    **/
    public static String getImageStr(File file) {
        //待处理的图片
//        String imgFile = "C:\\Users\\Administrator\\Desktop\\a\\1.png";
        InputStream in = null;
        byte[] data = null;
        //读取图片字节数组
        try {
            in = new FileInputStream(file);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw new ErrorCodesException("图片转换字符串失败",ErrorCodesException.IMG);
        }
        //对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        //返回Base64编码过的字节数组字符串
        return encoder.encode(data);
    }

}


